#include "sort.h"
#include "Stack.h"

void PrintArr(int* arr, int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void Swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}

void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n-1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0)
		{
			if (arr[end] > tmp)
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}

void BubbleSort(int* arr, int n)
{
	for (int i = 0; i < n; i++)
	{
		int exchange = 0;
		for (int j = 0;j < n-i-1; j++)
		{
			if (arr[j] > arr[j + 1])
			{
				exchange = 1;
				int tmp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = tmp;
			}
			if (exchange = 0)
			{
				break;
			}
		}
	}
}

void ADJustDown(int* arr, int parent, int n)
{
	int child = parent * 2 + 1;//左孩子
	while (child < n)
	{
		if (child + 1 < n && arr[child] > arr[child + 1])
		{
			child++;
		}
		if (arr[child] < arr[parent])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 - 1;
		}
		else
		{
			break;
		}
	}
}

void HeapSort(int* arr, int n)
{
	//向下调整算法建堆（大堆）
	for (int i = (n - 2) / 2; i >= 0; i--)
	{
		ADJustDown(arr, i, n);
	}
	//降序（用大堆+向下调整算法）
	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		ADJustDown(arr, 0, end);
		end--;
	}
}

void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap>1)
	{
		gap = (gap / 3)+1;
			for (int i = 0; i < n - gap; i++)
			{
				int end = i;
				int tmp = arr[end + gap];
				while (end >= 0)
				{
					if (arr[end] > tmp)
					{
						arr[end + gap] = arr[end];
						end -= gap;
					}
					else
					{
						break;
					}
					arr[end + gap] = tmp;
				}
			}
	}
}


void SelectSort(int* arr, int n)
{
	for (int i = 0; i < n; i++)
	{
		int mini = i;
		int max = i;
		//找最小的
		for (int j = i+1; j < n; j++)
		{
			if (arr[j] < arr[mini])
			{
				mini = j;
			}
		}
		//找到了最小值，i和mini的位置进行交换
		Swap(&arr[i], &arr[mini]);
	}
}

void SelectSort_2(int* arr, int n)
{
	int begin = 0;
	int end = n - 1;

	while ( begin < end )
	{
		int mini = begin;
		int maxi = begin;
		for (int i = begin; i <= end; i++)
		{
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
		}
		if (maxi == begin)
		{
			maxi = mini;
		}
		Swap(&arr[mini], &arr[begin++]);
		Swap(&arr[maxi], &arr[end--]);
	}
}

int _QuickSort(int* arr, int left, int right)
{
	int keyi = left;
	++left;
	while (left<=right)
	{
		while (arr[right] > arr[keyi] && left <= right )
		{
			right--;
		}
		//right找到比基准值小，或者刚好等于
		while (arr[left] < arr[keyi] && left <= right )
		{
			left++;
		};
		//left找到比基准值大，或者刚好等于
		if (left <= right)
		{
			Swap(&arr[left++], &arr[right--]);
		}
	}
	Swap(&arr[keyi], &arr[right]);
	return right;
}

int __QuickSort(int* arr, int left, int right)
{
	int hole = left;
	int key = arr[hole];
	while (left < right)
	{
		while (left < right && arr[right] > key)
		{
			right--;
		}
		arr[hole] = arr[right];
		hole = right;
		while (left < right && arr[left] < key)
		{
			left++;
		}
		arr[hole] = arr[left];
		hole = left;
	}
	arr[hole] = key;
	return hole;
}

int ___QuickSort(int* arr, int left, int right)
{
	int keyi = left;
	int prev = left;
	int cur = left + 1;
	while (cur <= right)
	{
		if (arr[cur] < arr[keyi] && ++prev != cur)
		{
			Swap(&arr[cur], &arr[prev]);
		}
		cur++;
	}
	Swap(&arr[keyi], &arr[prev]);
	return prev;
}

void QuickSort(int* arr, int left, int right)
{
	if (left >= right)
	{
		return;
	}
	int key = __QuickSort(arr, left, right);//找到基准值
	//左子序列：[left,key-1]
	QuickSort(arr,left,key-1);
	//右子序列：[key+1,right]
	QuickSort(arr,key+1,right);
}

<<<<<<< HEAD
void _MergeSort(int* arr, int left, int right, int* tmp)
=======
void QuickSortNonR(int* arr, int left, int right)
{
	ST st;
	STInit(&st);
	StackPush(&st, right);
	StackPush(&st, left);
	while (!StackEmpty(&st))
	{
		//取栈顶元素，取两次
		int begin = StackTop(&st);
		StackPop(&st);

		int end = StackTop(&st);
		StackPop(&st);

		//找基准值
		int prev = begin;
		int cur = begin + 1;
		int keyi = begin;

		while (cur <= end)
		{
			if (arr[cur] <= arr[keyi] && prev++ != cur)
			{
				Swap(&arr[cur], &arr[prev]);
			}
			cur++;
		}
		Swap(&arr[keyi], &arr[prev]);

		keyi = prev;
		//根据基准值划分左右区间
		//左区间：[begin,keyi-1]
		//右区间：[keyi+1，end]
		if ( keyi+1 < end )
		{
			StackPush(&st, end);
			StackPush(&st, keyi + 1);
		}
		if (keyi-1 > begin)
		{
			StackPush(&st, keyi-1);
			StackPush(&st, begin);
		}
	}
	STDestroy(&st);
}

void _MergeSort(int* arr,int left,int right,int* tmp)
>>>>>>> b9f58ba8556c0d73e144f332ee299f3be2d606dd
{
	if (left >= right)
	{
		return;
	}
	int mid = (left + right) / 2;
	//[left,mid]
	//[mid+1,right]
	_MergeSort(arr, left, mid, tmp);
<<<<<<< HEAD
	_MergeSort(arr, mid + 1, right, tmp);
=======
	_MergeSort(arr, mid+1, right, tmp);
>>>>>>> b9f58ba8556c0d73e144f332ee299f3be2d606dd

	//合并
	int begin1 = left;
	int end1 = mid;

	int begin2 = mid + 1;
	int end2 = right;

	int index = begin1;

	while (begin1 <= end1 && begin2 <= end2)
	{
		if (arr[begin1] < arr[begin2])
		{
			tmp[index++] = arr[begin1++];
		}
		else
		{
<<<<<<< HEAD
			tmp[index++] = arr[begin2++];
=======
			tmp[index++] = arr[begin2];
>>>>>>> b9f58ba8556c0d73e144f332ee299f3be2d606dd
		}
	}
	//begin1越界或者begin2越界
	while (begin1 <= end1)
	{
		tmp[index++] = arr[begin1++];
	}
	while (begin2 <= end2)
	{
<<<<<<< HEAD
		tmp[index++] = arr[begin2++];
	}
	//把tmp的数据拷贝的arr中
	for (int i = left; i <= right; i++)
=======
		tmp[index++] = arr[begin2];
	}
	//把tmp的数据拷贝的arr中
	for (int i = left; i < right; i++)
>>>>>>> b9f58ba8556c0d73e144f332ee299f3be2d606dd
	{
		arr[i] = tmp[i];
	}

}

void MergeSort(int* arr, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
<<<<<<< HEAD
	_MergeSort(arr, 0, n - 1, tmp);
=======

	_MergeSort(arr,0,n-1,tmp);



>>>>>>> b9f58ba8556c0d73e144f332ee299f3be2d606dd
	free(tmp);
}